package cn.tudu.coolshark.filter;

import cn.tudu.coolshark.pojo.vo.UserVO;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "MyFilter",urlPatterns = {"/admin.html","/insertProduct.html"})
public class MyFilter implements Filter {
    //初始化
    public void init(FilterConfig config) throws ServletException {
    }
//销毁
    public void destroy() {

    }
//执行过滤器,当请求经过时执行的方法
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest rt=(HttpServletRequest) request;
        HttpServletResponse re=(HttpServletResponse) response;
        //获取session
        HttpSession session=rt.getSession();
        UserVO user=(UserVO) session.getAttribute("user");
        if(user!=null){
        chain.doFilter(request, response);//放行
        }else{
            re.sendRedirect("/login.html");
        }
    }
}
